{
 "metadata": {},
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Matplotlib: loading a colormap dynamically\n",
      "======================================================================\n",
      "\n",
      "In a\n",
      "[thread](http://thread.gmane.org/gmane.comp.python.matplotlib.general/1536)\n",
      "on the matplotlib mailling list, James Boyle posted a way to load\n",
      "colormaps from a file. Here it is slightly modified.\n",
      "\n",
      "**gmtColormap.py**"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def gmtColormap(fileName,GMTPath = None):\n",
      "      import colorsys\n",
      "      import Numeric\n",
      "      N = Numeric\n",
      "      if type(GMTPath) == type(None):\n",
      "          filePath = \"/usr/local/cmaps/\"+ fileName+\".cpt\"\n",
      "      else:\n",
      "          filePath = GMTPath+\"/\"+ fileName +\".cpt\"\n",
      "      try:\n",
      "          f = open(filePath)\n",
      "      except:\n",
      "          print \"file \",filePath, \"not found\"\n",
      "          return None\n",
      " \n",
      "      lines = f.readlines()\n",
      "      f.close()\n",
      " \n",
      "      x = []\n",
      "      r = []\n",
      "      g = []\n",
      "      b = []\n",
      "      colorModel = \"RGB\"\n",
      "      for l in lines:\n",
      "          ls = l.split()\n",
      "          if l[0] == \"#\":\n",
      "             if ls[-1] == \"HSV\":\n",
      "                 colorModel = \"HSV\"\n",
      "                 continue\n",
      "             else:\n",
      "                 continue\n",
      "          if ls[0] == \"B\" or ls[0] == \"F\" or ls[0] == \"N\":\n",
      "             pass\n",
      "          else:\n",
      "              x.append(float(ls[0]))\n",
      "              r.append(float(ls[1]))\n",
      "              g.append(float(ls[2]))\n",
      "              b.append(float(ls[3]))\n",
      "              xtemp = float(ls[4])\n",
      "              rtemp = float(ls[5])\n",
      "              gtemp = float(ls[6])\n",
      "              btemp = float(ls[7])\n",
      " \n",
      "      x.append(xtemp)\n",
      "      r.append(rtemp)\n",
      "      g.append(gtemp)\n",
      "      b.append(btemp)\n",
      " \n",
      "      nTable = len(r)\n",
      "      x = N.array( x , N.Float)\n",
      "      r = N.array( r , N.Float)\n",
      "      g = N.array( g , N.Float)\n",
      "      b = N.array( b , N.Float)\n",
      "      if colorModel == \"HSV\":\n",
      "         for i in range(r.shape[0]):\n",
      "             rr,gg,bb = colorsys.hsv_to_rgb(r[i]/360.,g[i],b[i])\n",
      "             r[i] = rr ; g[i] = gg ; b[i] = bb\n",
      "      if colorModel == \"HSV\":\n",
      "         for i in range(r.shape[0]):\n",
      "             rr,gg,bb = colorsys.hsv_to_rgb(r[i]/360.,g[i],b[i])\n",
      "             r[i] = rr ; g[i] = gg ; b[i] = bb\n",
      "      if colorModel == \"RGB\":\n",
      "          r = r/255.\n",
      "          g = g/255.\n",
      "          b = b/255.\n",
      "      xNorm = (x - x[0])/(x[-1] - x[0])\n",
      " \n",
      "      red = []\n",
      "      blue = []\n",
      "      green = []\n",
      "      for i in range(len(x)):\n",
      "          red.append([xNorm[i],r[i],r[i]])\n",
      "          green.append([xNorm[i],g[i],g[i]])\n",
      "          blue.append([xNorm[i],b[i],b[i]])\n",
      "      colorDict = {\"red\":red, \"green\":green, \"blue\":blue}\n",
      "      return (colorDict)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}